Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
	''' <summary>
	''' Strongly-typed collection for the Messages class.
	''' </summary>
	<Serializable> _
	Public Partial Class MessagesCollection 
	Inherits ActiveList(Of Messages, MessagesCollection)
	    Public Sub New()
		End Sub
	End Class
	''' <summary>
	''' This is an ActiveRecord class which wraps the Messages table.
	''' </summary>
	<Serializable> _
	Public Partial Class Messages 
	Inherits ActiveRecord(Of Messages)
		#Region ".ctors and Default Settings"
		
		Public Sub New()
			SetSQLProps()
			InitSetDefaults()
			MarkNew()
		End Sub
		
		Public Sub New(ByVal useDatabaseDefaults As Boolean)
			SetSQLProps()
			If useDatabaseDefaults = True Then
				ForceDefaults()
			End If
			MarkNew()
		End Sub
		Public Sub New(ByVal keyID As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByKey(keyID)
		End Sub
		Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByParam(columnName,columnValue)
		End Sub
		Private Sub InitSetDefaults()
			SetDefaults()
		End Sub
		
		Protected Shared Sub SetSQLProps()
			GetTableSchema()
		End Sub
		#End Region
		
		#Region "Schema and Query Accessor"
		
		Public Shared ReadOnly Property Schema() As TableSchema.Table
			Get
				If BaseSchema Is Nothing Then
					SetSQLProps()
				End If
				Return BaseSchema
			End Get
		End Property
		Private Shared Sub GetTableSchema()
			If (Not IsSchemaInitialized) Then
				'Schema declaration
				Dim schema As TableSchema.Table = New TableSchema.Table("Messages", TableType.Table, DataService.GetInstance("ClubStarterKitData"))
				schema.Columns = New TableSchema.TableColumnCollection()
				schema.SchemaName = "dbo"
				'columns
				
                
                Dim colvarId As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarId.ColumnName = "id"
                colvarId.DataType = DbType.Int32
                colvarId.MaxLength = 0
                colvarId.AutoIncrement = true
                colvarId.IsNullable = false
                colvarId.IsPrimaryKey = true
                colvarId.IsForeignKey = false
                colvarId.IsReadOnly = false
                
                
                schema.Columns.Add(colvarId)
                
                Dim colvarMemberid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarMemberid.ColumnName = "memberid"
                colvarMemberid.DataType = DbType.Guid
                colvarMemberid.MaxLength = 0
                colvarMemberid.AutoIncrement = false
                colvarMemberid.IsNullable = false
                colvarMemberid.IsPrimaryKey = false
                colvarMemberid.IsForeignKey = false
                colvarMemberid.IsReadOnly = false
                
                
                schema.Columns.Add(colvarMemberid)
                
                Dim colvarPostdate As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarPostdate.ColumnName = "postdate"
                colvarPostdate.DataType = DbType.DateTime
                colvarPostdate.MaxLength = 0
                colvarPostdate.AutoIncrement = false
                colvarPostdate.IsNullable = false
                colvarPostdate.IsPrimaryKey = false
                colvarPostdate.IsForeignKey = false
                colvarPostdate.IsReadOnly = false
                
						colvarPostdate.DefaultSetting = "(getdate())"
				
                
                schema.Columns.Add(colvarPostdate)
                
                Dim colvarSubject As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarSubject.ColumnName = "subject"
                colvarSubject.DataType = DbType.String
                colvarSubject.MaxLength = 255
                colvarSubject.AutoIncrement = false
                colvarSubject.IsNullable = false
                colvarSubject.IsPrimaryKey = false
                colvarSubject.IsForeignKey = false
                colvarSubject.IsReadOnly = false
                
						colvarSubject.DefaultSetting = "('No Subject')"
				
                
                schema.Columns.Add(colvarSubject)
                
                Dim colvarBody As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarBody.ColumnName = "body"
                colvarBody.DataType = DbType.String
                colvarBody.MaxLength = 1073741823
                colvarBody.AutoIncrement = false
                colvarBody.IsNullable = true
                colvarBody.IsPrimaryKey = false
                colvarBody.IsForeignKey = false
                colvarBody.IsReadOnly = false
                
                
                schema.Columns.Add(colvarBody)
                
                Dim colvarThreadid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarThreadid.ColumnName = "threadid"
                colvarThreadid.DataType = DbType.Int32
                colvarThreadid.MaxLength = 0
                colvarThreadid.AutoIncrement = false
                colvarThreadid.IsNullable = false
                colvarThreadid.IsPrimaryKey = false
                colvarThreadid.IsForeignKey = true
                colvarThreadid.IsReadOnly = false
                
                
				colvarThreadid.ForeignKeyTableName = "Threads"
                
                schema.Columns.Add(colvarThreadid)
                
                Dim colvarParentmsgid As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarParentmsgid.ColumnName = "parentmsgid"
                colvarParentmsgid.DataType = DbType.Int32
                colvarParentmsgid.MaxLength = 0
                colvarParentmsgid.AutoIncrement = false
                colvarParentmsgid.IsNullable = false
                colvarParentmsgid.IsPrimaryKey = false
                colvarParentmsgid.IsForeignKey = false
                colvarParentmsgid.IsReadOnly = false
                
						colvarParentmsgid.DefaultSetting = "((0))"
				
                
                schema.Columns.Add(colvarParentmsgid)
                
                Dim colvarAnswer As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarAnswer.ColumnName = "answer"
                colvarAnswer.DataType = DbType.Boolean
                colvarAnswer.MaxLength = 0
                colvarAnswer.AutoIncrement = false
                colvarAnswer.IsNullable = false
                colvarAnswer.IsPrimaryKey = false
                colvarAnswer.IsForeignKey = false
                colvarAnswer.IsReadOnly = false
                
                
                schema.Columns.Add(colvarAnswer)
                
				BaseSchema = schema
				
				'add this schema to the provider
                'so we can query it later
                DataService.Providers("ClubStarterKitData").AddSchema("Messages",schema)
			End If
		End Sub
		Public Shared Function CreateQuery() As Query
			Return New Query(Schema)
		End Function
		
		#End Region
		
		#Region "Props"
	
          
        <XmlAttribute("Id")> _
        Public Property Id As Integer 
			Get
				Return GetColumnValue(Of Integer)("id")
			End Get
		    
			Set
				SetColumnValue("id", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Memberid")> _
        Public Property Memberid As Guid 
			Get
				Return GetColumnValue(Of Guid)("memberid")
			End Get
		    
			Set
				SetColumnValue("memberid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Postdate")> _
        Public Property Postdate As DateTime 
			Get
				Return GetColumnValue(Of DateTime)("postdate")
			End Get
		    
			Set
				SetColumnValue("postdate", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Subject")> _
        Public Property Subject As String 
			Get
				Return GetColumnValue(Of String)("subject")
			End Get
		    
			Set
				SetColumnValue("subject", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Body")> _
        Public Property Body As String 
			Get
				Return GetColumnValue(Of String)("body")
			End Get
		    
			Set
				SetColumnValue("body", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Threadid")> _
        Public Property Threadid As Integer 
			Get
				Return GetColumnValue(Of Integer)("threadid")
			End Get
		    
			Set
				SetColumnValue("threadid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Parentmsgid")> _
        Public Property Parentmsgid As Integer 
			Get
				Return GetColumnValue(Of Integer)("parentmsgid")
			End Get
		    
			Set
				SetColumnValue("parentmsgid", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("Answer")> _
        Public Property Answer As Boolean 
			Get
				Return GetColumnValue(Of Boolean)("answer")
			End Get
		    
			Set
				SetColumnValue("answer", Value)
			End Set
		End Property
		
		
		
		
		#End Region
		
		
			    
	    
		
		
		
		
		
	    
	    #Region "ForeignKey Methods"
	    
		''' <summary>
		''' Returns a Threads ActiveRecord object related to this Messages
		''' </summary>
		Public Property Threads() As Data.Threads
			Get
				Return Data.Threads.FetchByID(Me.Threadid)
			End Get
			Set
				SetColumnValue("threadid", Value.Threadid)
			End Set
		End Property
	    
	    #End Region
	    
		
	    
	    'no ManyToMany tables defined (0)
	    
		
		#Region "ObjectDataSource support"
		
		''' <summary>
		''' Inserts a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Insert(ByVal varMemberid As Guid,ByVal varPostdate As DateTime,ByVal varSubject As String,ByVal varBody As String,ByVal varThreadid As Integer,ByVal varParentmsgid As Integer,ByVal varAnswer As Boolean)
			Dim item As Messages = New Messages()
			
            item.Memberid = varMemberid
            
            item.Postdate = varPostdate
            
            item.Subject = varSubject
            
            item.Body = varBody
            
            item.Threadid = varThreadid
            
            item.Parentmsgid = varParentmsgid
            
            item.Answer = varAnswer
            
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		''' <summary>
		''' Updates a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Update(ByVal varId As Integer,ByVal varMemberid As Guid,ByVal varPostdate As DateTime,ByVal varSubject As String,ByVal varBody As String,ByVal varThreadid As Integer,ByVal varParentmsgid As Integer,ByVal varAnswer As Boolean)
			Dim item As Messages = New Messages()
		    
                item.Id = varId
				
                item.Memberid = varMemberid
				
                item.Postdate = varPostdate
				
                item.Subject = varSubject
				
                item.Body = varBody
				
                item.Threadid = varThreadid
				
                item.Parentmsgid = varParentmsgid
				
                item.Answer = varAnswer
				
			item.IsNew = False
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		#End Region
		#Region "Columns Struct"
		Public Structure Columns
			Dim x as Integer
			
            Public Shared Id As String = "id"
            
            Public Shared Memberid As String = "memberid"
            
            Public Shared Postdate As String = "postdate"
            
            Public Shared Subject As String = "subject"
            
            Public Shared Body As String = "body"
            
            Public Shared Threadid As String = "threadid"
            
            Public Shared Parentmsgid As String = "parentmsgid"
            
            Public Shared Answer As String = "answer"
            
		End Structure
		#End Region
	End Class
End Namespace
